<?php
##==========================================================
function db_insert_post($_POST)
{
		global $wpdb;
		$customerName		= $_POST['customerName'];
		$customerEmail		= $_POST['customerEmail'];
		$customerAddress	= $_POST['customerAddress'];
		$customerPhone		= $_POST['customerPhone'];		
		$customerMessage	= $_POST['elm1'];
		
		
		
		 // Create post object
		  $my_post = array(
		     'post_title' 		=> wp_strip_all_tags($customerName),
		     'post_content' 	=> $customerMessage,
		  	 'post_type' 		=> 'testimonials',
		     'post_status' 		=> 'publish',
		     'post_author' 		=> 1		     
		  );
		  
		 $post_id = wp_insert_post( $my_post ); 
		 if($post_id)
		 {			
			 update_post_meta($post_id, $wpdb->prefix."customer_phone", $customerPhone);
			 update_post_meta($post_id, $wpdb->prefix."customer_address", $customerAddress);
			 update_post_meta($post_id, $wpdb->prefix."customer_email", $customerEmail);	
			 update_post_meta($post_id, $wpdb->prefix."author", 'customer');
			 update_post_meta($post_id, $wpdb->prefix."display", 'hidden');		 
		 }
		 return $post_id;
}

function db_upload_photo($post_id,$_FILES,$meta_key)
{		
		 global $wpdb;
		 $overrides = array('test_form' => false);
		 $wp_upload_dir = wp_upload_dir();
		 $subdir = substr($wp_upload_dir['subdir'], 1);
		 $file = wp_handle_upload($_FILES, $overrides);
		 $filename		= $subdir.'/'.$_FILES['name'];
		
		 $wp_filetype = wp_check_filetype(basename($filename), null );
		  
		 $attachment = array(
		     'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $filename ), 
		     'post_mime_type' => $wp_filetype['type'],
		     'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
		     'post_content' => '',
		     'post_status' => 'inherit'
		  );
		  $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
		  //update_post_meta($post_id, $wpdb->prefix.$meta_key, $attach_id);
		  add_post_meta($post_id, $wpdb->prefix.$meta_key, $attach_id);
		  // you must first include the image.php file
		  // for the function wp_generate_attachment_metadata() to work		  
		  $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
		  wp_update_attachment_metadata( $attach_id, $attach_data );
	  
}

function db_check_file_type_upload($_FILES,$total,$file_name)
{
	$flag = true;
	for($i=0;$i<$total;$i++)
	{
		$type		= $_FILES['type'][$i];
		if($type!="image/gif" && $type!="image/jpg" && $type!="image/jpeg" && $type!="image/png" && $type!="image/bmp" && $type!="image/tif")
			return false;
	}
	return $flag;
}

function db_check_size_upload($_FILES,$total,$file_name,$total_size)
{
	$flag = true;
	$size = 0;
	for($i=0;$i<$total;$i++)
	{
		$size += $_FILES['size'][$i];
	}
	if($size>$total_size)
		return false;
	else 	
		return $flag;
}

function db_upload_multiple_photo($_FILES,$file_name,$post_id)
{
	//print_r($_FILES);
	//echo '<br/>';
	$loixayra = '';
	
	$countfile = count($_FILES['name']); // dem so luong file
	if($countfile>10)
	{
		$loixayra .= '\n Maximum 10 photos';
	}
	if(!db_check_file_type_upload($_FILES,$countfile,$file_name))
	{
		$loixayra .= '\n File type not valid. (jpg,jpeg,png,bmp,gif)';
	}	
	if(!db_check_size_upload($_FILES,$countfile,$file_name,10000000))
	{
		$loixayra .= '\n File size Maximum 10MB';
	}	
	
	$memfile_array = array();	
	if($loixayra=='')
	{
		
		//print_r($_FILES[$file_name]);
		for($i=0;$i<$countfile;$i++)
		{				
				$name		= $_FILES['name'][$i];
				$type		= $_FILES['type'][$i];
				$tmp_name	= $_FILES['tmp_name'][$i];
				$error		= $_FILES['error'][$i];
				$size		= $_FILES['size'][$i];
				
			$memfile = array(
			'name'		=> $name,
			'type'		=> $type,
			'tmp_name'	=> $tmp_name,
			'error'		=> $error,
			'size'		=> $size
			);
			$memfile_array[] = $memfile;		
		}	
		//print_r($_FILES);
		$step=1;
		foreach ($memfile_array as $value) 
		{
			//if($step==1) // upload anh minh hoa
				//db_upload_photo($post_id,$value,'thumbnail');
				
			db_upload_photo($post_id,$value,'thumbnail2');
			$step++;
		}
		$loixayra = "Thank you for send testimonial widht us.";
			
	}
	return $loixayra;	
}

$result_error = null;
if(wp_verify_nonce($_POST["act_send_testimonial"],"act_send_testimonial"))
{
	if($_POST['captcha']==$_SESSION['ma_xac_nhan'])
	{
		//print_r($_FILES['avatar']);
		$post_id = db_insert_post($_POST);
		if($post_id)
		{	
			// Upload Avatar photo
			db_upload_photo($post_id,$_FILES['avatar'],"thumbnail");
		 	$result_error = db_upload_multiple_photo($_FILES['photos'],'photos',$post_id);
		 	$postID = $post_id;
		 	require_once TEMPLATEPATH.'/mails/xtemplate-email-testimonial.php';
		 	require_once TEMPLATEPATH.'/mails/xtemplate-email-testimonial-admin.php';
		}
		$_SESSION['ma_xac_nhan'] = null;
	}
	else 
	{
		$result_error = 'The code invalid, Please try again.';
	}
}